package com.lanwf.admin;

import com.jfinal.core.Controller;
import com.jfinal.core.NotAction;
import com.jfinal.kit.StrKit;
import com.lanwf.common.model.SysAdmin;
import com.lanwf.common.model.SysSession;
import com.lanwf.admin.utils.Cons;

public class BaseController extends Controller {

	/**
	 * 获取cookie
	 * @return
	 */
	public String getCookie(){
		return getCookie(Cons.ADMIN_COOKIE);
	}

	public SysAdmin getAccount() {
		String cookie = getCookie();
		SysSession first = (SysSession)SysSession.dao.findFirst("select * from sys_session where id = ? ", cookie);
		return !StrKit.isBlank(cookie) && first != null ? (SysAdmin)SysAdmin.dao.findById(first.getAccountId()) : null;
	}

	public Integer getAccountId() {
		return this.getAccount() == null ? -1 : this.getAccount().getId();
	}


	/**
	 * 当前请求是否为 ajax 请求
	 */
	@NotAction
	public boolean isAjaxRequest() {
		return "XMLHttpRequest".equalsIgnoreCase(getHeader("X-Requested-With"));
	}
}


